var selected_K_line_stock_name = '';
var selected_K_line_ts_code = '';
var selected_K_line_K_code = '';


/*
窗体加载后执行函数
 */
function when_onload() {
    selected_K_line_stock_name = $("#p_stock_name").text();
    selected_K_line_ts_code = $("#p_ts_code").text();

    symbol = selected_K_line_ts_code.split('.')[0].trim();
    shzs = selected_K_line_ts_code.split('.')[1].trim();
    selected_K_line_K_code = shzs.toLowerCase() + symbol;

    //应该按时间，一分钟更新一次
    get_k_line_from_sina();

    window.setInterval(get_k_line_from_sina, 600000);
    // window.setInterval(get_chart_moneyflow_from_eastmoney, 600000);


    // get_chart_moneyflow_from_eastmoney();
    // get_data_moneyflow();
}

window.onload = when_onload();


/*
从sina网获取K线图
*/
function get_k_line_from_sina() {

    // 在股票代码前面，即sz000001
    time_k = 'http://image.sinajs.cn/newchart/min/n/' + selected_K_line_K_code + '.gif';
    daily_k = 'http://image.sinajs.cn/newchart/daily/n/' + selected_K_line_K_code + '.gif';
    weekly_k = 'http://image.sinajs.cn/newchart/weekly/n/' + selected_K_line_K_code + '.gif';
    monthly_k = 'http://image.sinajs.cn/newchart/monthly/n/' + selected_K_line_K_code + '.gif';

    // $("#img_time_K_line").attr('src', time_k);
    $("#img_time_K_line").attr({src:time_k, width:"400" , height:"300" });
    $("#img_daily_K_line").attr({src:daily_k , width:"400" , height:"300"});
    $("#img_week_K_line").attr({src:weekly_k , width:"400", height:"300" });
    $("#img_month_K_line").attr({src:monthly_k , width:"400", height:"300" });

}


/*
从东方财富网获取资金流入流出图形
 */
function get_chart_moneyflow_from_eastmoney() {
    $.ajax({
        url: '/stock/get_selected_stock_K_line/',
        type: "GET",
        data: {'request_cmd': 'chart_moneyflow', 'ts_code': selected_K_line_K_code},
        success: function (result) {
            $("#div_chart_money_flow").empty();

            $("#div_chart_money_flow").html(result);
        },
        error: function (e) {
            // alert(e);
        }
    });
}


/*
获取主力净流入数据
 */
function get_data_moneyflow() {
    $.ajax({
        url: '/stock/get_selected_stock_K_line/',
        type: "GET",
        data: {'request_cmd': 'data_moneyflow', 'ts_code': selected_K_line_ts_code},
        success: function (result) {
            $("#div_data_moneyflow").empty();
            $("#div_data_moneyflow").append("<div style='text-align: center'><em>" + result.trade_date + "主力资金流向</em></div>");
            $("#div_data_moneyflow").append("<table align='center' border='1'><tbody id='id_tbody'></tbody></table>");
            table = $("#id_tbody");
            table.append("<tr id='id_tr1' align='center'></tr>");
            //table.lastChild.appendChild("<th></th>");
            table_th = $("#id_tr1");
            table_th.append("<th>主力净流入<br>单位万元</th>");
            table_th.append("<th>主力占换手率比例<br>单位%</th>");
            main_in = ((result.buy_elg_amount + result.buy_lg_amount - result.sell_elg_amount - result.sell_lg_amount)).toFixed(2);
            main_turnover_rate = ((result.buy_elg_vol + result.buy_lg_vol) * 100 / (result.buy_elg_vol + result.buy_lg_vol + result.buy_md_vol + result.buy_sm_vol)).toFixed(2);
            table.append("<tr id='id_tr2' align='center'></tr>");
            table_th = $("#id_tr2");
            table_th.append("<td>" + main_in + "</td>");
            table_th.append("<td>" + main_turnover_rate + "</td>");


        },
        error: function (e) {
            alert(e);
        }
    });

}

